#include <sxQCameraFrame.h>
#include <QToolButton>
#include <QToolBar>

Sx::QtInterface::QCameraFrame::QCameraFrame(QWidget* parent, QViewport* viewport, const std::shared_ptr<ViewportController>& controller, Qt::WFlags flags) :
	QViewportFrame(parent, viewport, controller, flags) {
	this->view = nullptr;

	this->points = nullptr;
	this->wireframe = nullptr;
	this->solid = nullptr;
	this->firstPerson = nullptr;
	this->thirdPerson = nullptr;
	this->left = nullptr;
	this->right = nullptr;
	this->top = nullptr;
	this->bottom = nullptr;
	this->front = nullptr;
	this->back = nullptr;

	this->constructToolbar();
}

Sx::QtInterface::QCameraFrame::~QCameraFrame() {}

bool Sx::QtInterface::QCameraFrame::constructToolbar() {
	if ( this->toolbar != nullptr ) {
		std::cout << "[QCameraFrame:constructToolbar] Error: Toolbar already constructed." << std::endl;
		return false;
	}

	//--------------------------------------------------------------------------
	// Construct the toolbar of this ViewportFrame. This toolbar will hold all
	// of the buttons that allow the user to change the view and display of
	// the objects in the viewport this frame contains.
	//--------------------------------------------------------------------------
	this->toolbar = new QToolBar(this);
	this->toolbar->setIconSize(QSize(this->toolbarIconSize, this->toolbarIconSize));

	//--------------------------------------------------------------------------
	// Construct the buttons that define how the objects in the viewport can
	// be rendered.
	//--------------------------------------------------------------------------
	ConstructToolButton(this->points, this->toolbar, this, "points", "Point Display", SLOT(onPoints()));
	ConstructToolButton(this->wireframe, this->toolbar, this, "wireframe", "Wireframe Display", SLOT(onWireframe()));
	ConstructToolButton(this->solid, this->toolbar, this, "solid", "Solid Display", SLOT(onSolid()));
	this->toolbar->addSeparator();

	//--------------------------------------------------------------------------
	// Construct the buttons that allow the user to select between a first
	// person camera and a third person camera.
	//--------------------------------------------------------------------------
	ConstructToolButton(this->firstPerson, this->toolbar, this, "firstperson", "First Person", SLOT(onFirstPerson()));
	ConstructToolButton(this->thirdPerson, this->toolbar, this, "thirdperson", "Third Person", SLOT(onThirdPerson()));
	this->toolbar->addSeparator();

	//--------------------------------------------------------------------------
	// Construct the buttons that allow the user to select an orthographic
	// view of the scene.
	//--------------------------------------------------------------------------
	ConstructToolButton(this->left, this->toolbar, this, "left", "Orthographic Left", SLOT(onLeft()));
	ConstructToolButton(this->right, this->toolbar, this, "right", "Orthographic Right", SLOT(onRight()));
	ConstructToolButton(this->top, this->toolbar, this, "top", "Orthographic Top", SLOT(onTop()));
	ConstructToolButton(this->bottom, this->toolbar, this, "bottom", "Orthographic Bottom", SLOT(onBottom()));
	ConstructToolButton(this->front, this->toolbar, this, "front", "Orthographic Front", SLOT(onFront()));
	ConstructToolButton(this->back, this->toolbar, this, "back", "Orthographic Back", SLOT(onBack()));

	return true;
}

void Sx::QtInterface::QCameraFrame::setCameraView(const std::shared_ptr<Interface_CameraView>& view) {
	this->view = view;
}

void Sx::QtInterface::QCameraFrame::onPoints() {
	if ( this->view == nullptr ) return;
	this->view->setDisplayMode(Graphics::POINTS);
}

void Sx::QtInterface::QCameraFrame::onWireframe() {
	if ( this->view == nullptr ) return;
	this->view->setDisplayMode(Graphics::WIREFRAME);
}

void Sx::QtInterface::QCameraFrame::onSolid() {
	if ( this->view == nullptr ) return;
	this->view->setDisplayMode(Graphics::SOLID);
}

void Sx::QtInterface::QCameraFrame::onFirstPerson() {
	if ( this->view == nullptr ) return;
	this->view->setProjection(Math::PERSPECTIVE);
	this->view->setPerspective(Math::FIRST_PERSON);
}

void Sx::QtInterface::QCameraFrame::onThirdPerson() {
	if ( this->view == nullptr ) return;
	this->view->setProjection(Math::PERSPECTIVE);
	this->view->setPerspective(Math::THIRD_PERSON);
}

void Sx::QtInterface::QCameraFrame::onLeft() {
	if ( this->view == nullptr ) return;
	this->view->setProjection(Math::ORTHOGRAPHIC);
	this->view->setView(Math::LEFT);
}

void Sx::QtInterface::QCameraFrame::onRight() {
	if ( this->view == nullptr ) return;
	this->view->setProjection(Math::ORTHOGRAPHIC);
	this->view->setView(Math::RIGHT);
}

void Sx::QtInterface::QCameraFrame::onTop() {
	if ( this->view == nullptr ) return;
	this->view->setProjection(Math::ORTHOGRAPHIC);
	this->view->setView(Math::TOP);
}

void Sx::QtInterface::QCameraFrame::onBottom() {
	if ( this->view == nullptr ) return;
	this->view->setProjection(Math::ORTHOGRAPHIC);
	this->view->setView(Math::BOTTOM);
}

void Sx::QtInterface::QCameraFrame::onFront() {
	if ( this->view == nullptr ) return;
	this->view->setProjection(Math::ORTHOGRAPHIC);
	this->view->setView(Math::FRONT);
}

void Sx::QtInterface::QCameraFrame::onBack() {
	if ( this->view == nullptr ) return;
	this->view->setProjection(Math::ORTHOGRAPHIC);
	this->view->setView(Math::BACK);
}
